package com.base.api.service.commodityClassify.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.base.api.dto.commodityClassify.CommodityClassifyListDTO;
import com.base.api.dto.commodityClassify.CommodityClassifySearchDTO;
import com.base.api.service.commodityClassify.CommodityClassifyApiService;
import com.base.api.vo.commodity.BountyListVO;
import com.base.result.PageUtils;
import com.base.result.ResponseResult;
import com.base.result.ResponseResultUtils;
import com.base.self.entity.Commodity;
import com.base.self.entity.CommodityClassify;
import com.base.self.mapper.CommodityClassifyMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Optional;

@Service
public class CommodityClassifyApiServiceImpl implements CommodityClassifyApiService {

    private final CommodityClassifyMapper commodityClassifyMapper;

    public CommodityClassifyApiServiceImpl(CommodityClassifyMapper commodityClassifyMapper) {
        this.commodityClassifyMapper = commodityClassifyMapper;
    }

    @Override
    @Transactional(readOnly = true)
    public ResponseResult commodityClassifyList(CommodityClassifyListDTO commodityClassifyListDTO) {
        final Integer classifyPid = commodityClassifyListDTO.getClassifyPid();
        CommodityClassify commodityClassify = commodityClassifyMapper.selectOne(
                new LambdaQueryWrapper<CommodityClassify>()
                        .eq(CommodityClassify::getId, classifyPid)
                        .eq(CommodityClassify::getIsDel, 0)
                        .orderByDesc(CommodityClassify::getClassifySort)
        );
        List<CommodityClassify> data = commodityClassifyMapper.selectList(new LambdaQueryWrapper<CommodityClassify>()
                .eq(CommodityClassify::getIsDel, 0)
                .eq(Optional.ofNullable(classifyPid).isPresent(), CommodityClassify::getClassifyPid, classifyPid)
                .orderByDesc(CommodityClassify::getClassifySort)
        );
        Optional.ofNullable(commodityClassify).ifPresent(x -> {
            data.forEach(i -> i.setClassifyPidName(commodityClassify.getClassifyName()));
        });

        return ResponseResultUtils.getResponseResultDataS(data);
    }

    @Override
    public ResponseResult commodityClassifySearch(CommodityClassifySearchDTO commodityClassifySearchDTO) {
        final Map<String, Object> data = PageUtils.getDateMap(() ->
                        commodityClassifyMapper.selectPage(
                                PageUtils.getPage(
                                        new Page<>(),
                                        commodityClassifySearchDTO.getOffset(),
                                        commodityClassifySearchDTO.getLimit()
                                ),
                                new LambdaQueryWrapper<CommodityClassify>()
                                        .eq(CommodityClassify::getIsDel, 0)
                                        .likeRight(CommodityClassify::getClassifyName, commodityClassifySearchDTO.getCommodityName())
                        )
                );
        return ResponseResultUtils.getResponseResultS("查询成功", data);
    }

}
